from django.db import models

from apps.member.models import Member
from apps.system.models import Upload, Option
from utils.exceptions import CustomError
from apps.member.stores.models import Store


class Activity(models.Model):
    PREHEAT_ING = 0
    BEGIN_ING = 1
    FINISH = 2
    STATUS_TYPE = (
        (PREHEAT_ING, '预热'),
        (BEGIN_ING, '进行中'),
        (FINISH, '已结束'),
    )

    VOTE = 1
    BARGAIN = 2
    ACTIVITY_TYPE = (
        (VOTE, '投票'),
        (BARGAIN, '砍价')
    )
    activity_type = models.PositiveSmallIntegerField(choices=ACTIVITY_TYPE, verbose_name='活动类型')
    merchant = models.ForeignKey(Member, verbose_name='商家', on_delete=models.PROTECT, blank=True)
    store = models.ForeignKey(Store, verbose_name='商铺', on_delete=models.PROTECT, blank=True)
    title = models.CharField(max_length=200, verbose_name='活动名称')
    theme = models.ForeignKey(Option, verbose_name='活动主题', on_delete=models.PROTECT, related_name='theme')
    start_time = models.DateTimeField(verbose_name='活动开始时间')
    end_time = models.DateTimeField(verbose_name='活动结束时间')
    images = models.ManyToManyField(Upload, verbose_name='活动图片')
    rules = models.CharField(max_length=1000, verbose_name='活动规则')
    content = models.TextField(verbose_name='活动详情')
    status = models.PositiveSmallIntegerField(choices=STATUS_TYPE, verbose_name='活动状态', default=PREHEAT_ING)
    brown_count = models.IntegerField(verbose_name='浏览量', default=0)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    delete = models.BooleanField(verbose_name='是否删除', default=False)

    class Meta:
        db_table = 'activity'
        verbose_name = '活动信息'
        ordering = ('-create_time',)

    @staticmethod
    def get_instance_by_id(id):
        try:
            id = int(id)
        except:
            raise CustomError('无效的activity_id')
        instance = Activity.objects.filter(pk=id).first()
        if not instance:
            raise CustomError('未找到对应的活动实例')
        return instance

    def is_apply_activity(self, member):
        # 判断用户是否已经报名
        is_apply = MemberActivity.objects.filter(activity=self, member=member)
        if is_apply:
            return True
        return False


class ActivityCeleryTask(models.Model):
    activity = models.ForeignKey(Activity, verbose_name='活动', on_delete=models.PROTECT)
    # 定时任务id
    start_task_id = models.CharField(max_length=100, verbose_name='活动开始celery任务ID')
    end_task_id = models.CharField(max_length=100, verbose_name='活动结束celery任务ID')

    class Meta:
        db_table = 'activity_celery_task'
        verbose_name = '活动定时任务id'
        ordering = ('-id',)


class MemberActivity(models.Model):
    BEGIN_ING = 1
    FINISH = 2
    STATUS_TYPE = (
        (BEGIN_ING, '进行中'),
        (FINISH, '已结束'),
    )
    status = models.PositiveSmallIntegerField(choices=STATUS_TYPE, verbose_name='活动状态', default=BEGIN_ING)
    member = models.ForeignKey(Member, verbose_name='用户', on_delete=models.PROTECT)
    activity = models.ForeignKey(Activity, verbose_name='活动', on_delete=models.PROTECT)
    create_time = models.DateTimeField(verbose_name='报名时间', auto_now_add=True)

    class Meta:
        db_table = 'member_activity'
        verbose_name = '用户报名活动'
        ordering = ('-create_time',)


class CollectActivity(models.Model):
    member = models.ForeignKey(Member, verbose_name='用户', on_delete=models.PROTECT)
    activity = models.ForeignKey(Activity, verbose_name='活动', on_delete=models.PROTECT)
    create_time = models.DateTimeField(verbose_name='收藏时间', auto_now_add=True)

    class Meta:
        db_table = 'activity_collect'
        verbose_name = '收藏活动'
        ordering = ('-create_time',)
